namespace CSharpPro._1016;

public class Demo_cs2
{
    // 7. 用户登录与注册
    public void Test7()
    {
        Console.WriteLine("=== 用户登录与注册 ===");
        Dictionary<string, string> users = new Dictionary<string, string>();
        while (true)
        {
            Console.WriteLine("\n主菜单：1. 登录  2. 注册  3. 退出");
            Console.Write("请选择功能：");
            string choice = Console.ReadLine();
            
            switch (choice)
            {
                case "1":
                    Login(users);
                    break;
                case "2":
                    Register(users);
                    break;
                case "3":
                    return;
                default:
                    Console.WriteLine("输入错误，请重新输入！");
                    break;
            }
        }
    }

    static void Register(Dictionary<string, string> users)
    {
        Console.Write("请输入用户名：");
        string username = Console.ReadLine();
        if (users.ContainsKey(username))
        {
            Console.WriteLine("用户名已存在！");
            return;
        }
        Console.Write("请输入密码：");
        string password = Console.ReadLine();
        Console.Write("请确认密码：");
        string confirmPassword = Console.ReadLine();
        if (password != confirmPassword)
        {
            Console.WriteLine("两次密码不一致！");
            return;
        }
        users.Add(username, password);
        Console.WriteLine("注册成功！");
    }

    static void Login(Dictionary<string, string> users)
    {
        Console.Write("请输入用户名：");
        string username = Console.ReadLine();
        if (!users.ContainsKey(username))
        {
            Console.WriteLine("用户名不存在！");
            return;
        }
        Console.Write("请输入密码：");
        string password = Console.ReadLine();
        if (users[username] == password)
        {
            Console.WriteLine("登录成功！进入系统首页（模拟）");
        }
        else
        {
            Console.WriteLine("密码错误！");
        }
    }
}